package com.itheima.dao;

import com.itheima.domain.Student;
import com.itheima.utils.JDBCUtils;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class StudentDaoImpl implements StudentDaoInter {
    private ArrayList<Student> list = new ArrayList<>();
    Connection con = JDBCUtils.getConnection();
    PreparedStatement ps = null;
    ResultSet res = null;
    Student stu = null;
    @Override
    public ArrayList<Student> findAll() {
        try {
            ps = con.prepareStatement("select * from student");
            res = ps.executeQuery();
            while(res.next()){
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                list.add(new Student(sid,name,age,birthday));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,ps,res);
        }
        return list;
    }

    @Override
    public Student findById(Integer id) {
        String sql = "select * from student where sid=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setObject(1,id);
            res = ps.executeQuery();
            if(res.next()){
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                stu = new Student(sid,name,age,birthday);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,ps,res);
        }
        return stu;
    }

    @Override
    public int inert(Student stu) {
        int res = 0;
        String sql= "insert into student value (?,?,?,?)";
        try {
            ps = con.prepareStatement(sql);
            ps.setObject(1,stu.getSid());
            ps.setObject(2,stu.getName());
            ps.setObject(3,stu.getAge());
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(stu.getBirthday());
            ps.setObject(4,birthday);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,ps);
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        int res = 0;
        String sql = "delete from student where sid=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setObject(1,id);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,ps);
        }
        return res;
    }

    @Override
    public int update(Student stu,Integer id) {
        int res = 0;
        String sql = "update student set sid=?,name=?,age=?,birthday=? where sid=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setObject(1,stu.getSid());
            ps.setObject(2,stu.getName());
            ps.setObject(3,stu.getAge());
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(stu.getBirthday());
            ps.setObject(4,birthday);
            ps.setObject(5,id);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,ps);
        }
        return res;
    }
}
